/**
 * Copyright By Grandsoft Company Limited.  
 * 2013-6-13 上午11:30:05
 */
package com.glodon.ggzy.jypt.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.Constants;
import com.glodon.ggzy.core.utils.JsonUtils;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.entity.GgZfBuyregform;
import com.glodon.ggzy.entity.GgZfContract;
import com.glodon.ggzy.entity.GgZfOrder;
import com.glodon.ggzy.fwpt.service.IZfcgProjectService;
import com.glodon.ggzy.vo.ProjectSubway;
import com.glodon.ggzy.vo.ZfcgContract;
import com.glodon.ggzy.vo.ZfcgDataInfo;
import com.glodon.ggzy.vo.ZfcgOrder;
import com.glodon.ggzy.vo.buyRegStatus;

/**
 * 
 * 采购人处理控制类
 * @author yangjf
 * @since jdk1.6
 * @date 2013-6-13
 *  
 */

@Controller
public class CgrController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IZfcgProjectService zfcgProjectService;

	/**
	 *  采购人-主页
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/cgr_index")
	public String cgrIndex(Model model, HttpServletRequest request) {
		String transNum = zfcgProjectService.getCgrSuccessTransNumber(request);
		String amountNum = zfcgProjectService.getCgrContractAmountNumber(request);
		model.addAttribute("transNum", transNum);
		model.addAttribute("amountNum", amountNum);
		return "/jypt/cgr_index";
	}

	/**
	 * 我的订单
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/cgr_my_order")
	public String cgrMyOrder(Model model) {
		return "/jypt/cgr_my_order";
	}

	/**
	 * 我的合同
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/cgr_my_contract")
	public String cgrProjectEnter(Model model) {
		return "/jypt/cgr_my_contract";
	}

	/**
	 * 我的采购登记单
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/cgr_my_buyreg")
	public String cgrMyBuyreg(Model model) {
		return "/jypt/cgr_my_buyreg";
	}

	@RequestMapping("/jypt/cgr_my_buyreg_data")
	public void loadCgrBuyRegDataInfo(Model model, int rows, int page, String noOrName, HttpServletResponse response, HttpServletRequest request) {
		PageSearchModel psm = new PageSearchModel();
		psm.setPageNo(page);
		psm.setPageSize(rows);
		String jsonStr = "";
		Page queryPage = zfcgProjectService.queryBuyregPage(psm, noOrName, request);
		if (queryPage != null) {
			List<GgZfBuyregform> buyList = (List<GgZfBuyregform>) queryPage.getResult();
			List<ZfcgDataInfo> buyDataList = new ArrayList<ZfcgDataInfo>();
			ZfcgDataInfo buyData = null;
			for (GgZfBuyregform buy : buyList) {
				String status = StringUtils.isEmpty(buy.getStatus()) ? "" : buyRegStatus.valueOf(buy.getStatus()).getName();
				buyData = new ZfcgDataInfo(buy.getId(), buy.getRegformcode(), buy.getRegformname(), buy.getBuyform(), status, buy.getCreatetime());
				buyDataList.add(buyData);
			}
			queryPage.setResult(buyDataList);
			try {
				jsonStr = JsonUtils.toJsonStr(queryPage);
				response.getWriter().write(jsonStr);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@RequestMapping("/jypt/cgr_my_order_data")
	public void loadCgrOrderDataInfo(Model model, int rows, int page, String noOrName, HttpServletResponse response, HttpServletRequest request) {
		PageSearchModel psm = new PageSearchModel();
		psm.setPageNo(page);
		psm.setPageSize(rows);
		String jsonStr = "";
		Page queryPage = zfcgProjectService.queryOrderPage(psm, noOrName, true, request);
		if (queryPage != null) {
			List<GgZfOrder> orderList = (List<GgZfOrder>) queryPage.getResult();
			List<ZfcgOrder> orderDataList = new ArrayList<ZfcgOrder>();
			ZfcgOrder orderData = null;
			for (GgZfOrder order : orderList) {
				orderData = new ZfcgOrder(order.getId(), order.getOrdercode(), order.getOrdertype(), order.getStatus(), order.getCreatetime(),
						order.getBuyername(), order.getSellername(), "", order.getOrdersum());
				orderDataList.add(orderData);
			}
			queryPage.setResult(orderDataList);
			try {
				jsonStr = JsonUtils.toJsonStr(queryPage);
				response.getWriter().write(jsonStr);
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * 
	 * 我的合同
	 * @param model
	 * @return
	 */
	@RequestMapping("/jypt/load_cgr_my_contract_data")
	public void cgrMyContract(Model model, int rows, int page, String noOrName, HttpServletResponse response, HttpServletRequest request) {
		PageSearchModel queryModel = new PageSearchModel();
		queryModel.setPageNo(page);
		queryModel.setPageSize(rows);
		String jsonStr = "";
		Page queryPage = zfcgProjectService.queryContractPage(queryModel, Constants.ZF_CGR, noOrName, request);
		if (queryPage != null) {
			List<GgZfContract> contractList = (List<GgZfContract>) queryPage.getResult();
			List<ZfcgContract> contractDisplayList = new ArrayList<ZfcgContract>();
			ZfcgContract contract = null;
			for (GgZfContract c : contractList) {
				contract = new ZfcgContract(c.getContractcode(), c.getContractname(), c.getBuyername(), c.getSellername(), c.getStatus(),
						c.getContractsum());
				contractDisplayList.add(contract);
			}
			queryPage.setResult(contractDisplayList);
			try {
				jsonStr = JsonUtils.toJsonStr(queryPage);
				response.getWriter().write(jsonStr);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 
	 * 采购人地铁站
	 * @param model
	 * @return
	 */
	@Deprecated
	@RequestMapping("/jypt/cgr_project_subway")
	public String cgrProjectSubway(Model model) {

		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		try {
			ProjectSubway way = new ProjectSubway(df.parse("2013年4月21日 09时21分"), df.parse("2013年4月30日 09时00分"), df.parse("2013年5月08日 09时00分"),
					df.parse("2013年5月15日 09时21分"), df.parse("2013年5月30日 09时00分"), "false", "", "未编辑", "", "", "3");
			model.addAttribute("pway", way);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "/jypt/cgr_project_subway";
	}

}
